/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UpdateEmployeeInfoList.java
 *
 * Created on Nov 15, 2011, 5:56:09 PM
 */

/**
 *
 * @author Marco Angelo
 */

import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.proteanit.sql.DbUtils;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;

public class ViewEmployeeInfo extends javax.swing.JFrame {

    public ViewEmployeeInfo() {
        initComponents();
        this.setBounds(400, 100, 850, 350);
        this.setTitle("Employee Information List");
        df = new SimpleDateFormat("yyyy-MM-dd");
    }

   PreparedStatement pst = null;
   PreparedStatement pst2 = null;
   ResultSet rs = null;
   Connection conn = null;
   Statement stat1;
   DateFormat df;

    private void UpdateJTable(){
    String sql = "SELECT * FROM employeeinfo";
    try{
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        txtHiring.setDateFormatString("yyyy-MM-dd");
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
    }
    catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        UpdateBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        removeBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        viewInactiveBtn = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtFName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtLName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtPosition = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtID = new javax.swing.JLabel();
        txtDept = new javax.swing.JComboBox();
        txtHiring = new com.toedter.calendar.JDateChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Employee ID", "First Name", "Last Name", "Position", "Department", "Hiring Date", "Password"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        UpdateBtn.setText("Update");
        UpdateBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                UpdateBtnMouseClicked(evt);
            }
        });
        UpdateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpdateBtnActionPerformed(evt);
            }
        });

        cancelBtn.setText("Cancel");
        cancelBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelBtnMouseClicked(evt);
            }
        });

        removeBtn.setText("Remove");
        removeBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                removeBtnMouseClicked(evt);
            }
        });

        jLabel1.setText("Employee Information");

        viewInactiveBtn.setText("View Inactive");
        viewInactiveBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                viewInactiveBtnMouseClicked(evt);
            }
        });
        viewInactiveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewInactiveBtnActionPerformed(evt);
            }
        });

        jLabel3.setText("First Name:");

        jLabel4.setText("Last Name:");

        jLabel5.setText("Position:");

        jLabel6.setText("Department:");

        jLabel7.setText("Hiring Date:");

        jLabel8.setText("Password:");

        jLabel2.setText("Employee ID:");

        txtID.setText("0");

        txtDept.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sales", "Office", "Distribution" }));
        txtDept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDeptActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(171, 171, 171)
                .addComponent(UpdateBtn)
                .addGap(55, 55, 55)
                .addComponent(removeBtn)
                .addGap(54, 54, 54)
                .addComponent(viewInactiveBtn)
                .addGap(63, 63, 63)
                .addComponent(cancelBtn)
                .addGap(217, 217, 217))
            .addGroup(layout.createSequentialGroup()
                .addGap(348, 348, 348)
                .addComponent(jLabel1)
                .addContainerGap(421, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 772, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(78, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(txtDept, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(txtID)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtHiring, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtFName, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPassword)
                            .addComponent(txtLName, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txtPosition, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(89, 89, 89))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(txtPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtFName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(txtID)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtHiring, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7)
                                .addComponent(txtDept, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtLName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))))
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(removeBtn)
                    .addComponent(viewInactiveBtn)
                    .addComponent(cancelBtn)
                    .addComponent(UpdateBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void UpdateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdateBtnActionPerformed
     
    }//GEN-LAST:event_UpdateBtnActionPerformed

    private void UpdateBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UpdateBtnMouseClicked
        String sql = "UPDATE employeeinfo SET FirstName = ?, LastName = ?, Position = ?, Department = ?, HiringDate = ?, Password = ? WHERE EmployeeID = ?";
        try{
            pst = conn.prepareStatement(sql);

            pst.setString(1, txtFName.getText());
            pst.setString(2, txtLName.getText());
            pst.setString(3, txtPosition.getText());
            pst.setString(4, txtDept.getSelectedItem().toString());
            pst.setString(5, df.format(txtHiring.getDate()));
            pst.setString(6, txtPassword.getText());
            pst.setString(7, txtID.getText());

            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Successfully updated employee info!");
            UpdateJTable();          
        }
        catch (Exception e){
           JOptionPane.showMessageDialog(null, e); 
        }
    }//GEN-LAST:event_UpdateBtnMouseClicked

    private void cancelBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelBtnMouseClicked
        new AdminScreen2().setVisible(true);
        this.setVisible(false);
}//GEN-LAST:event_cancelBtnMouseClicked

    private void viewInactiveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewInactiveBtnActionPerformed

    }//GEN-LAST:event_viewInactiveBtnActionPerformed

    private void removeBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_removeBtnMouseClicked
      
        int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to remove this employee?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

        int empID = Integer.parseInt(txtID.getText());
        String empFname = (txtFName.getText());
        String empLname = (txtLName.getText());
        String empDept = (txtDept.getSelectedItem().toString());
        String empPosition = (txtPosition.getText());
        String empPassword = (txtPassword.getText());

        float empBSalary = (Float.parseFloat(ViewEmployeeSalary.txtBaseSalary.getText()));
        float empSSS = (Float.parseFloat(ViewEmployeeSalary.txtSSS.getText()));
        float empPgIBIGLoan = (Float.parseFloat(ViewEmployeeSalary.txtpagIBIGLoan.getText()));
        float empPgIBIGContract = (Float.parseFloat(ViewEmployeeSalary.txtpagIBIGContract.getText()));

        int month1 = txtHiring.getDate().getMonth() + 1;
        int year1 = txtHiring.getDate().getYear() + 1900;
        int day1 = txtHiring.getDate().getDate();

        if (response == JOptionPane.YES_OPTION) {
            try{
                stat1 = conn.createStatement();
                stat1.executeUpdate("INSERT INTO `medmartpayroll_db`.`inactive_employeeinfo` (`EmployeeID`, `FirstName`, `LastName`, `Position`, `Department`, `HiringDate`, `Password`) VALUES ('"+ empID +"', '"+ empFname +"', '"+ empLname +"', '"+ empDept +"', '"+ empPosition +"', '"+ year1 + "-"  + month1 + "-" + day1 +"', '"+ empPassword +"')");
                stat1.executeUpdate("DELETE FROM `medmartpayroll_db`.`employeeinfo` where `EmployeeID` = '" + empID + "'");
                stat1.executeUpdate("INSERT INTO `medmartpayroll_db`.`inactive_salaryinfo` (`EmployeeID`, `FirstName`, `LastName`, `BaseSalary`, `SSS`, `PagIBIGLoan`, `PagIBIGContract`, `DateReceived`) VALUES ('"+ empID +"', '"+ empFname +"', '"+ empLname +"', '"+ empBSalary +"', '"+ empSSS +"', '"+ empPgIBIGLoan +"', '"+ empPgIBIGContract +"', '"+ year1 + "-"  + month1 + "-" + day1 +"')");
                stat1.executeUpdate("DELETE FROM `medmartpayroll_db`.`salaryinfo` where `EmployeeID` = '" + empID + "'");
                UpdateJTable();
                JOptionPane.showMessageDialog(null, "Successfully removed employee info!");
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        else if (response == JOptionPane.CLOSED_OPTION) {
            System.out.println("JOptionPane closed");
        }
    }//GEN-LAST:event_removeBtnMouseClicked

    private void viewInactiveBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_viewInactiveBtnMouseClicked
        new ViewInactiveEmployeeInfo().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_viewInactiveBtnMouseClicked

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        conn = Connector.get();
        UpdateJTable();
    }//GEN-LAST:event_formWindowOpened

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        
        int row = jTable1.getSelectedRow();
        txtID.setText(jTable1.getModel().getValueAt(row, 0).toString());
        txtFName.setText(jTable1.getModel().getValueAt(row, 1).toString());
        txtLName.setText(jTable1.getModel().getValueAt(row, 2).toString());
        txtPosition.setText(jTable1.getModel().getValueAt(row, 3).toString());
        txtDept.setSelectedItem(jTable1.getModel().getValueAt(row, 4));
        try {
            txtHiring.setDateFormatString("yyyy-MM-dd");
            txtHiring.setDate(df.parse(jTable1.getModel().getValueAt(row, 5).toString()));
        } catch (ParseException ex) {
            Logger.getLogger(ViewEmployeeInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
        txtPassword.setText(jTable1.getModel().getValueAt(row, 6).toString());
    }//GEN-LAST:event_jTable1MouseClicked

    private void txtDeptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDeptActionPerformed
       
    }//GEN-LAST:event_txtDeptActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewEmployeeInfo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton UpdateBtn;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton removeBtn;
    public static javax.swing.JComboBox txtDept;
    public static javax.swing.JTextField txtFName;
    public static com.toedter.calendar.JDateChooser txtHiring;
    public static javax.swing.JLabel txtID;
    public static javax.swing.JTextField txtLName;
    public static javax.swing.JTextField txtPassword;
    public static javax.swing.JTextField txtPosition;
    private javax.swing.JButton viewInactiveBtn;
    // End of variables declaration//GEN-END:variables

}
